package com.kang.controller;

import com.kang.Vo.GroupVo;
import com.kang.commons.Result;
import com.kang.bean.GroupS;
import com.kang.bean.Users;
import com.kang.commons.util.MapToObjUtil;
import com.kang.service.GroupService;
import com.kang.service.UserService;
import com.kang.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @program: photo_fawu
 * @description: 分组控制层
 * @author: K.faWu
 * @create: 2021-10-27 11:26
 **/

@RestController
@RequestMapping("/group")
public class GroupController {
    @Autowired
    private GroupService groupService;

    @Autowired
    private UserService userService;

    @Autowired
    private UsersService usersService;

    /**
     * 修改相册
     */
    @PutMapping("/update")
    public Result<?> update(@RequestBody GroupS groupS){
        groupService.update(groupS);
        return Result.success();
    }

    /**
     * 根据id获取相册的密码
     * ## 前提是前来获取的用户是这个相册的所有者
     */
    @GetMapping("getPassById/{id}")
    public Result<?> getPassById(@PathVariable Long id, HttpServletRequest request){
        String password = groupService.getPassById(id, request);
        return Result.success(password);
    }

    /**
     * 判断该相册是否属于当前用户
     */
    @GetMapping("isByGroupId/{id}")
    public Result isById(@PathVariable Long id, HttpServletRequest request) throws ClassNotFoundException {
        Result result = usersService.getUsers2(request.getHeader("token"));
        Users users= MapToObjUtil.map2Obj((Map<String, Object>) result.getData(), Users.class);
        groupService.isById(id,users);
        return Result.success();
    }

    /**
     * 查询用户所拥有的公开、加密的相册
     */
    @GetMapping("/{userId}")
    public Result getByUserId(@PathVariable Long userId){
        List<GroupVo> list = groupService.getByUserId(userId);
        return Result.success(list);
    }

    /**
     * 查询全部相册
     * @return
     */
    @GetMapping
    public Result getAll(HttpServletRequest request){
        List<GroupVo> list = groupService.getAll(request);
        return Result.success(list);
    }

    /**
     * 添加相册
     * @param group
     * @return
     */
    @PostMapping
    public Result addGroup(@RequestBody GroupS group, HttpServletRequest request){
        GroupS result = groupService.add(group, request);
        return Result.success(result);
    }

    /**
     * 删除相册
     */
    @DeleteMapping
    public Result deleteGroup(Long id){
        groupService.deleteGroup(id);
        return Result.success();
    }

}
